class InputTool{
    constructor(input,inpNum){
        this.content = input;
        this.loadTypeCallback = this.loadImage;
        this.onload = this.onload;
        this.result = [];
        this.loadOverNum = 0;
        this.status = 0;
        this.inpNumEl = inpNum
        this.init();
    }
    isLoadIng(){
        return this.status == 1;
    }
    setType(type){
        if(type == "image"){
            this.loadTypeCallback = this.loadImage
        }
    }
    init(){
        this.content.addEventListener("change",()=>{
            let input = this.content
            var file = input.files[0];
            if(!file){
                return;
            }
            this.loadOverNum = 0;
            this.result = [];
            var files = input.files;
            this.status = 1;
            this.inpNumEl.innerHTML = files.length+"";
            for(let i = 0;i<files.length;i++){
                var file = files[i];
                var reader = new FileReader();
                reader.readAsDataURL(file);
                let j = i;
                reader.onload = (e)=>{
                    this.loadTypeCallback(e,j);
                }
            }
        })
    }
    loadImage(e,index){
        this.result;
        let texture = e.target.result;
        let img = new Image();
        img.src = texture;
        this.result.push({
            name:this.content.files[index].name.substr(0,this.content.files[index].name.lastIndexOf(".")),
            value:img
        })
        img.onload = ()=>{
            this.loadOverNum++;
            if(this.loadOverNum == this.content.files.length){
                this.onloaded(this.result);
            }
        }
    }
    onloaded(result){
        this.status = 0;
        this.onload && this.onload(result);
    }
}
module.exports = InputTool